// Coypright (C) 2024 wlly-lzh
// Createed by wlly-lzh on 24-5-22.
// 使用哈夫曼编码实现文本文件的压缩
#include <stdio.h>
#include <string.h>
#include "func.h"

int main(int argc,char *argv[])
{
	if(argc == 1) {
		version();
	}else if(argc == 2){
		if(strcmp(argv[1],"--help")==0 || strcmp(argv[1],"-h")==0 ){
			help(); // 提示信息
		}else if(strcmp(argv[1],"-v")==0 || strcmp(argv[1],"--version")==0 ){
			version();
		}else{
			option_error();
			return 2;
		}
	}else if(argc == 4){
		FILE * file=fopen(argv[1],"r");
		if(file == NULL) {
			file_not_exit();
			return 1;
		}
		if(strcmp(argv[2],"-o") == 0){
			FILE * outfile=fopen(argv[3],"wb");
			if(outfile != NULL){
				compress(file,outfile); // 压缩文件
			}
		} else{
			option_error();
			return 2;
		}
	}else if(argc == 5){ // hfmzip -X file1 -o file2
		if(strcmp(argv[1],"-X") != 0){
			option_error();
			return 2;
		}
		FILE * file=fopen(argv[2],"r");
		if(file == NULL){
			file_not_exit();
			return 1;
		}
		if(strcmp(argv[3],"-o") != 0){
			option_error();
			return 2;
		}
		FILE * outfile=fopen(argv[4],"w");
		uncompress(file,outfile);
	}else{
		option_error();
	}
	return 0;
}
